package com.ruoyi.enterprise.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.enterprise.service.PhoneService;
import org.springframework.stereotype.Service;

/**
 * description: 短信服务ServiceImpl
 * @author: gql
 * @date: 2022/08
 */
@Service
public class PhoneServiceImpl implements PhoneService {

    @Override
    public boolean sendMessage(String phone, String code) throws ClientException {
        return setParameter(phone, code, "", "SMS_186617568");
    }

    @Override
    public void sendSmsWithZero(String phone, String teamNumber) throws ClientException {
        setParameter(phone, "", teamNumber, "SMS_195720395");
    }

    /**
     * 初审通过
     * @param phone 手机号
     * @param teamNumber 队伍编号
     * @throws ClientException
     */
    @Override
    public void sendSmsWithTwo(String phone, String teamNumber) throws ClientException {
        setParameter(phone, "", teamNumber, "SMS_249455024");
    }


    /**
     * 初审待修改
     * @param phone 手机号
     * @param teamNumber 队伍编号
     * @throws ClientException
     */
    @Override
    public void sendSmsWithFour(String phone, String teamNumber) throws ClientException {
        setParameter(phone, "", teamNumber, "SMS_249580025");
    }

    @Override
    public void sendSmsWithFive(String phone, String teamNumber) throws ClientException {
        setParameter(phone, "", teamNumber, "SMS_182385259");
    }

    /**
     * 二审通过
     * @param phone 手机号
     * @param teamNumber 队伍编号
     * @throws ClientException
     */
    @Override
    public void sendSmsWithEight(String phone, String teamNumber) throws ClientException {
        setParameter(phone, "", teamNumber, "SMS_249600045");
    }

    /**
     * 二审待修改
     * @param phone 手机号
     * @param teamNumber 队伍编号
     * @throws ClientException
     */
    @Override
    public void sendSmsWithSix(String phone, String teamNumber) throws ClientException {
        setParameter(phone, "", teamNumber, "SMS_249440086");
    }

    /**
     * 三方发短信模板
     * @param phoneNumber 手机号
     * @param code 验证码
     * @param teamNumber 临时数字
     * @param signTemp 签名
     * @throws ClientException
     */
    private static boolean setParameter(String phoneNumber, String code, String teamNumber, String signTemp) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4FeQ6ZxAnbVBhhk6rSVj", "p2QjpoUzgwlphEeikf5p8YYMDFl1YZ");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", "智能制造大赛");
        request.putQueryParameter("TemplateCode", signTemp);
        if (!code.isEmpty()) {
            request.putQueryParameter("TemplateParam", "{\"code\":" + "\"" + code + "\"" + "}");
        } else if (!teamNumber.isEmpty()) {
            request.putQueryParameter("TemplateParam", "{\"teamNumber\":" + "\"" + teamNumber + "\"" + "}");
        }

        CommonResponse response = client.getCommonResponse(request);
        return stringToJson(response.getData()).equals(R.IS_OK);

    }

    public static String stringToJson(String s) {
        JSONObject jsonObject = JSON.parseObject(s);
        return jsonObject.getString("Code");
    }


}
